/*************************************************************************
 *                    free                     *
 *************************************************************************
 *************************************************************************
 * Filename	   : user_interface.c
 * Description   : This file contains the functions act with the user
 * Version	   : 1.0
 * History       :
 free 2013-4-17  Create
 *************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "table_pat.h"
#include "table_pmt.h"
#include "table_sdt.h"

/*************************************************************************
 * Function Name : GetServiceIdByIndex
 * Description   : get the service id from the program index
 * Parameters    :
 * Returns
 **************************************************************************/
static int GetServiceIdByIndex(int iProgramIndex, SECTION_SDT *pstTableSdt)
{
	int iProgramCounter = 0;
	SERVICE *pstServices = NULL;
	SERVICE_DESCRIPTOR *pstDescriptor = NULL;

	while (pstTableSdt != NULL )
	{
		pstServices = pstTableSdt->pstServices;
		while (pstServices != NULL )
		{
			pstDescriptor = pstServices->pstServiceDescriptors;
			if (pstDescriptor != NULL )
			{
				iProgramCounter++;
				if (iProgramCounter == iProgramIndex)
				{
					return pstServices->service_id;
				}
			}
			pstServices = pstServices->pstNext;
		}
		pstTableSdt = pstTableSdt->pstNext;
	}
	return -1;
}

/*************************************************************************
 * Function Name : GetParseTableResult
 * Description   : to show the result of parsing  psi tables
 * Parameters    :
 * Returns
 **************************************************************************/
int GetParseTableResult(SECTION_PAT *pstTablePat, PMT_TABLELIST *pstPmtTableList, SECTION_SDT *pstTableSdt)
{
	if (pstTablePat == NULL )
	{
		printf("No Program Association Table found !\n");
		return -1;
	}
	if (pstPmtTableList == NULL )
	{
		printf("No Program Map Table found !\n");
		return -1;
	}
	if (pstTableSdt == NULL )
	{
		printf("No Service Description Table found !\n");
		return -1;
	}
	return 1;
}

/*************************************************************************
 * Function Name : ShowAllPrograms
 * Description   : Show all the programs the sdt contained
 * Parameters    :
 * Returns
 **************************************************************************/
static void ShowAllPrograms(SECTION_SDT *pstTableSdt)
{
	int iProgramCounter = 0;
	SERVICE *pstServices = NULL;
	SERVICE_DESCRIPTOR *pstDescriptor = NULL;

	printf(" %-10s %-10s %-25s %-25s \n", "INDEX", "ID", "PROGRAM NAME", "PROVIDER");
	printf("-------------------------------------------------------------------------\n");
	while (pstTableSdt != NULL )
	{
		pstServices = pstTableSdt->pstServices;
		while (pstServices != NULL )
		{
			pstDescriptor = pstServices->pstServiceDescriptors;
			if (pstDescriptor != NULL )
			{
				printf(" %-10d %-10d %-25s %-25s \n", ++iProgramCounter, pstServices->service_id, pstDescriptor->pucServiceName,
				        pstDescriptor->pucServiceProviderName);
			}
			pstServices = pstServices->pstNext;
		}
		pstTableSdt = pstTableSdt->pstNext;
	}
	printf("-------------------------------------------------------------------------\n");
}

/*************************************************************************
 * Function Name : GetUserServiceId
 * Description   : to get the right service id from the user input
 * Parameters    :
 * Returns
 **************************************************************************/
int GetUserServiceId(SECTION_SDT *pstTableSdt)
{
	int iProgramIndex = 0;
	int iServiceId = 0;
	char aucBuffer[256] = {0};

	ShowAllPrograms(pstTableSdt);
	printf("\nPlease enter the program index you want to save:\n");
	for (;;)
	{
		printf("	[Q] -- quit	[P] -- program table\n");
		gets(aucBuffer);
		if (stricmp(aucBuffer, "q") == 0)
		{
			return -1;
		}
		if (stricmp(aucBuffer, "p") == 0)
		{
			ShowAllPrograms(pstTableSdt);
			printf("\n\nPlease enter the program index you want to save:\n");
			continue;
		}
		iProgramIndex = atoi(aucBuffer);
		if (iProgramIndex <= 0)
		{
			printf("\n\n\nPlease enter a number larger than 0:\n");
			continue;
		}
		iServiceId = GetServiceIdByIndex(iProgramIndex, pstTableSdt);
		if (iServiceId == -1)
		{
			printf("\n\n\nProgram index out of range, please enter again:\n");
			continue;
		}
		printf("\n\n\nAre you sure you want save program %d ?\n	[Y] -- yes	Any key -- back\n", iProgramIndex);
		gets(aucBuffer);
		if (stricmp(aucBuffer, "y") != 0)
		{
			printf("\n\n\nPlease enter the program index you want to save:\n");
			continue;
		}
		printf("\n\n\nCopy Process is start, please wait!\n");
		return iServiceId;
	}
	return -1;
}

/*************************************************************************
 * Function Name : ShowCopyFileResult
 * Description   : to show user the copy file result
 * Parameters    :
 * Returns
 **************************************************************************/
void ShowCopyFileResult(int iCopyFileResult)
{
	if (iCopyFileResult)
	{
		printf("Copy file succeed!\n");
	}
	else
	{
		printf("Copy file failed!\n");
	}
	printf("\n\nEnter any character to continue");
	getchar();
}

